package Items;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.PodcastQ.BD.BD;

public class item {
	
	public item() {
		super();
		listener = new OnClickListener(){ // the book's action
	        @Override
	        public void onClick(View v) {
	                            // the default action for al
	        	Descargado = "1";
	        	new DescargaSong().execute(id);
	        	
	        }
	    };
	}

	BD basededatoshelper;
	
	public OnClickListener listener;
    
    

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getItemDescription() {
		return itemDescription;
	}
	public void setItemDescription(String itemDescription) {
		this.itemDescription = itemDescription;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public Bitmap getImageNumber() {
		return imageNumber;
	}
	public void setImageNumber(Bitmap imageNumber) {
		this.imageNumber = imageNumber;
	}

	private String name ;
	private String itemDescription;
	private String price;
	private String urlSong;
	private Bitmap imageNumber;
	private String id;
	private Context contexto;
	private String Descargado;
	
	public String getUrlSong() {
		return urlSong;
	}
	public void setUrlSong(String urlSong) {
		this.urlSong = urlSong;
	}
	public String getDescargado() {
		return Descargado;
	}
	public void setDescargado(String descargado) {
		Descargado = descargado;
	}
	public Context getContexto() {
		return contexto;
	}
	public String getUrl() {
		return urlSong;
	}
	public void setUrl(String url) {
		this.urlSong = url;
	}
	public void setContexto(Context contexto) {
		this.contexto = contexto;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	

	private  class DescargaSong extends AsyncTask<String, Integer, String> {
		ProgressDialog mProgressDialog;
		String titulo;

		// instantiate it within the onCreate method

		@Override
		protected String doInBackground(String... params) {
			try{
				String idactual = params[0];

				URL url = new URL(urlSong);
				URLConnection conexion = url.openConnection();
				conexion.connect();
				// this will be useful so that you can show a tipical 0-100% progress bar
				int lenghtOfFile = conexion.getContentLength();
				String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
				InputStream input = new BufferedInputStream(url.openStream());
				File file = new File(extStorageDirectory, idactual+".mp3");
				OutputStream output = new FileOutputStream(file);

				byte data[] = new byte[1024];

				long total = 0;

				int count;
				while ((count = input.read(data)) != -1) {
					total += count;
					// publishing the progress....
					publishProgress((int)(total*100/lenghtOfFile));
					output.write(data, 0, count);
				}

				output.flush();
				output.close();
				input.close();

				basededatoshelper = new BD(contexto,"QCU", null, 6);
				SQLiteDatabase song2file = basededatoshelper.getWritableDatabase();
				song2file.execSQL("UPDATE PODCAST SET url='"+extStorageDirectory+"/"+idactual+".mp3', " +
						"descargado=1 where id="+idactual);
			}catch(Exception e){
				e.printStackTrace();
				return "KO";
			}
			return "OK";

		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			mProgressDialog.dismiss();
			if(result.equalsIgnoreCase("OK"))
				Toast.makeText(contexto, titulo+" DESCARGADO", Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(contexto, "DESCARGA "+ titulo +" FALLIDA", Toast.LENGTH_SHORT).show();
			

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			titulo = name;
			mProgressDialog = new ProgressDialog(contexto);
			mProgressDialog.setMessage("Descargando Podcast :\n"+titulo);
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.setCancelable(true);
			mProgressDialog.setMax(100);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgressDialog.show();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			mProgressDialog.setProgress(values[0]);
		}

	}


	

}


